package one.bugu.android.demon.ui.widget;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.*;

import com.juefeng.android.framework.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/11/29
 * Time: 16:19
 * Description:
 */
@SuppressLint("JavascriptInterface")
public class XWebView extends WebView {
    private static final int LOADING_FINISH = 100;
    private Context mContext;

    private OnWebListener onWebListener;

    public XWebView(Context context) {
        super(context);
        this.mContext = context;
        initWebView();
    }

    public XWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initWebView();
    }

    public XWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initWebView();
    }


    private void initWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setWebContentsDebuggingEnabled(true);
        }
        WebSettings webSetting = getSettings();
        /**
         * 启用mixed content    android 5.0以上默认不支持Mixed Content
         * 5.0以上允许加载http和https混合的页面(5.0以下默认允许，5.0+默认禁止)
         * */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setNeedInitialFocus(false);
        webSetting.setAllowContentAccess(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webSetting.setAllowFileAccessFromFileURLs(false);
            webSetting.setAllowUniversalAccessFromFileURLs(false);
        }
        webSetting.setAppCacheEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //支持缩放，默认为true。
        webSetting.setSupportZoom(false);
        //调整图片至适合webview的大小
        webSetting.setUseWideViewPort(true);
        // 缩放至屏幕的大小
        webSetting.setLoadWithOverviewMode(true);
        //设置默认编码
        webSetting.setDefaultTextEncodingName("utf-8");
        //设置webview可通过内存绘制
        this.setDrawingCacheEnabled(true);
        ////设置自动加载图片
        webSetting.setLoadsImagesAutomatically(true);
        addJavascriptInterface(new WebOperation(), "android");
        setWebViewClient(new WebViewClient());
    }

    /**
     * 上传成功
     * 调用js方法
     */
    public void uploadImgSuccess(String imageId, String imageCount, String imageUrl) {
        loadUrl("javascript:uploadImgSuccess('" + imageId + "','" + imageCount + "','" + imageUrl + "')");
    }

    public void setOnWebListener(OnWebListener onWebListener) {
        this.onWebListener = onWebListener;
    }

    private class WebOperation {


        @JavascriptInterface
        public void GoBack(String string) {
            if (onWebListener != null) {
                onWebListener.goBack();
            }
        }

        @JavascriptInterface
        public void closeBySelf(String str1) {
            if (onWebListener != null) {
                onWebListener.closeBySelf();
            }
        }

        @JavascriptInterface
        public void savaPic(String str) {
            if (onWebListener != null) {
                onWebListener.savePic(str);
            }
        }

        @JavascriptInterface
        public void shareUrl(String url) {
            if (onWebListener != null) {
                onWebListener.shareUrl(url);
            }
        }

        @JavascriptInterface
        public void copy(String info) {
            ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData data = ClipData.newPlainText("order", info);
            cm.setPrimaryClip(data);
            //ToastUtils.custom("复制成功");
        }

    }


    public interface OnWebListener {

        /**
         * 返回
         */
        void goBack();

        /**
         * 关闭当前界面，自己打开自己的商品界面
         */
        void closeBySelf();

        void savePic(String str);

        /**
         * 开奖分享
         */
        void shareUrl(String url);

    }


}
